;(function () {
  new Vue({
    el: '#house-sharing',
    data: {
      flag: false,
      projectList: [],
      buildingList: [],
      floorList: [],
      unitList: [],
      projectId: [],
      buildingId: [],
      floorId: [],
      unitId: [],
      beginTime: '',
      endTime: '',
      creator: [],
      orders: '',
      columns: 'LUT',
      currentPage: 1,
      totalPage: 0,
      totalCount: 0 ,
      limit: ['10'],
      pageCounts: [{name: '10', code: '10'}, {name: '15', code: '15'}, {name: '20', code: '20'}],
      listData: [],
      createRen: [],
      totalCount: 0,
      keyword: '',
      erweimaflag: false,
      quanxian: JSON.parse(window.localStorage.getItem('permissions')),
      groupId:null,
      groupOneData:[],
      groupTwoData:[],
      groupThreeData:[],
      selectedGroupData:[],
    },
    filters: {
      framtTime (time) {
        return hlDateUtil.formatDate(time,'yyyy-MM-dd HH:mm:ss')
      }
    },
    methods: {
			delInfo (item) {
				hllayer.confirm1('此操作无法撤回，确认删除？', () => {
					hlhttp.delete({
						url: 'customer/house_resource?id='+ item.houseResourceRd,
						success: () => {
							hl.toast('删除成功')
							this.getlistData()
						}
					})
				})
			},
			selectPerNum(arr) {
			  this.limit = [arr[0].code]
        this.currentPage = 1
			  this.getlistData()
			},
      //显示隐藏查询条件
      showHideTerm () {
        this.flag = !this.flag;
        this.projectId = [];
        this.buildingId = [];
        this.floorId = [];
        this.unitId = [];
      },
      //获取项目
      getProjectData () {
        hlhttp.get({
          url: 'user/projects',
          success: (res) => {
						res = hlArrayUtil.removeRepeat(res, 'projectId')
            this.projectList = hlArrayUtil.replaceData(res, [{key: 'projectName', rKey: 'name'}, {key: 'projectId', rKey: 'code'}]);
          }
        })
      },
      // 获取一级组合数据
      getGroupPrimaryData(data){
        const self = this
        self.projectId = [data[0].code]
        self.groupId = null
        self.buildingId = []
        self.floorId = []
        self.unitId = []
        self.buildingList = []
        self.floorList = []
        self.unitList = []
        self.selectedGroupData = []
        self.getGroupData().then(data => {
          self.groupOneData = hlArrayUtil.replaceData( data, [{key: 'id', rKey: 'code'}] );
        })

        self.getBuildingData()
      },
      // 选取一级组合数据
      selectOneGroup(res){
        const self = this
        this.groupId = res['cur']['code']

        this.getGroupData().then(data => {
          self.groupTwoData = hlArrayUtil.replaceData( data, [{key: 'id', rKey: 'code'}] )
          self.getBuildingData()
        })
      },
      // 选取二级组合数据
      selectTwoGroup(res){
        const self = this
        this.groupId = res['cur']['code']
        this.getGroupData().then(data => {
          self.groupThreeData = hlArrayUtil.replaceData( data, [{key: 'id', rKey: 'code'}] )
          self.getBuildingData()
        })
      },
      // 选取三级组合数据
      selectThreeGroup(res){
        this.groupId = res['cur']['code']
      },
      getGroupData(){
        const self = this

        return new Promise(resolve => {
          hlhttp.get({
            url:'project/grouping_info/_subclass',
            data:{'project_id':self.projectId[0] , 'grouping_id':self.groupId || '#' },
            success:function(data){
              resolve(data)
            }
          })
        })
      },
      //获取楼宇
      getBuildingData () {
        const self = this

        hlhttp.get({
          url: 'project/buildings',
          data: { project_id: self.projectId.length ? self.projectId[0] : '#'  , grouping_id:self.groupId || '#' },
          success: (res) => {
            this.buildingList = hlArrayUtil.replaceData(res, [{key: 'buildingId', rKey: 'code'}]);
          }
        })
      },
      //获取楼层
      getfloorData () {
        hlhttp.get({
          url: 'building/floors',
          data: {project_id: this.projectId[0], building_id: this.buildingId[0]},
          success: (res) => {
            this.floorList = hlArrayUtil.replaceData(res, [{key: 'floorName', rKey: 'name'}, {key: 'floorId', rKey: 'code'}]);
          }
        })
      },
      //获取单元
      getunitData () {
        hlhttp.get({
          url: 'floor/units',
          data: {project_id: this.projectId[0], building_id: this.buildingId[0], floor_id: this.floorId[0]},
          success: (res) => {
            this.unitList = hlArrayUtil.replaceData(res, [{key: 'unitName', rKey: 'name'}, {key: 'unitId', rKey: 'code'}]);
          }
        })
      },
      //点击楼宇获得 id
      getbuildingId (arr) {
        this.buildingId = [arr[0].code];
        this.getfloorData()
        this.floorId = [];
        this.unitId = [];
      },
      //点击楼层获得 id
      getfloorId (arr) {
        this.floorId = [arr[0].code];
        this.getunitData()
        this.unitId = [];
      },
      // 点击单元获得 id
      getunitId (arr) {
        this.unitId = [arr[0].code];
      },
      // 设置背景色
      addbgcolor (index) {
        let el = document.querySelectorAll('.house-two-tone');
        let el0 = document.querySelectorAll('.house-operate')
        el[index].classList.add('house-two-tone-hover')
        el0[index].classList.add('house-two-tone-hover')
      },
      //删除背景色
      removebgcolor (index) {
        let el = document.querySelectorAll('.house-two-tone');
        let el0 = document.querySelectorAll('.house-operate')
        el[index].classList.remove('house-two-tone-hover')
        el0[index].classList.remove('house-two-tone-hover')
      },
      routerGo () {
        hlUrlUtil.goTo('createHouse', {ddtab:true})
      },
      //最后更新时间
      updateTime () {
        if (this.orders == '') {
          this.orders = 'DESC'
          this.getlistData()
          return
        }
        if (this.orders == 'ASC') {
          this.orders = 'DESC'
          this.getlistData()
          return
        }
        if (this.orders == 'DESC') {
          this.orders = 'ASC'
          this.getlistData()
        }
      },
      // 查询
      search(){
        this.currentPage = 1
        this.limit = ['10']
        this.getlistData()
      },
      getlistData () {
        hlhttp.get({
          url: 'customer/search_house_resource',
          data: {
            keyword: this.keyword || '#',
            begin_time: this.beginTime ? this.beginTime + ' 00:00:00' : '#',
            end_time: this.endTime ? this.endTime + ' 23:59:59' : '#',
            project_id: this.projectId.length ? this.projectId[0] : '#',
            group_id: this.groupId || '#',
            building_id: this.buildingId.length ? this.buildingId[0] : '#',
            floor_id: this.floorId.length ? this.floorId[0] : '#',
            unit_id: this.unitId.length ? this.unitId[0] : '#',
            creator: this.creator.length ? this.creator[0] : '#',
            orders: this.orders || 'DESC',
            columns: this.columns,
            page: this.currentPage,
            limit: this.limit[0]
          },
          success: (res) => {
            this.listData = res.houseResources
            this.totalCount = res.totalCount
            this.totalPage = res.totalPage
          }
        })
      },
      createRens () {
        hlhttp.get({
          url: 'customer/search_hs_creator',
          success: (res) => {
            this.createRen = hlArrayUtil.replaceData(res, [{key: 'creatorName', rKey: 'name'}, {key: 'creatorId', rKey: 'code'}]);
          }
        })
      },
			//创建人选择
			createPeople (arr) {
			  this.creator = [arr[0].code];
			},
      resetData () {
        this.currentPage = 1
        this.projectId = []
        this.groupId = null
        this.buildingId = []
        this.floorId = []
        this.unitId = []
        this.beginTime = ''
        this.endTime = ''
        this.creator = []
        this.orders = ''
        this.buildingList = []
        this.floorList = []
        this.unitList = []
        this.selectedGroupData = []
        this.keyword = ''

        this.getlistData()
      },
      //生成二维码
      sharingWxCode (url) {
        this.erweimaflag = true
        setTimeout(() => {
          var qrcode = new QRCode('qrcode', {
            text: url,
            width: 121,
            height: 121,
            colorDark : '#000000',
            colorLight : '#ffffff',
            correctLevel : QRCode.CorrectLevel.H
          });

        }, 500)
      },
      saveImageDown () {
        let down = document.createElement('a')
        down.setAttribute('download', '房源二维码')
        down.setAttribute('href', $('#qrcode img').attr('src'))
        down.click()
        this.erweimaflag = false
      }
    },
    created () {
      hlSelectUi()
    },
    mounted () {
      this.getProjectData()
      this.createRens()
      this.getlistData()
    }
  })
} ())
